package com.cloud.cmt.cmtsso.controller;


import com.cloud.cmt.cmtsso.entity.UserInfo;
import com.cloud.cmt.cmtsso.service.impl.UserServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.StringUtils;
import org.apache.shiro.web.session.HttpServletSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Set;
/**
 * User: wcy
 * Date: 2017/9/12
 * Time: 11:37
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    UserServiceImpl userService;

    Logger logger = LoggerFactory.getLogger(LoginController.class);

    @RequestMapping("index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("/login");
        return mv;
    }

    @RequestMapping("out")
    public ModelAndView out() {
        SecurityUtils.getSubject().logout();
        ModelAndView view = new ModelAndView();
        view.setViewName("redirect:http://localhost:8080/cas/logout");
        return view;
    }

    @RequestMapping("auth")
    public ModelAndView auth(String username, String password, boolean rememberMe, HttpSession session) {
        ModelAndView mv = new ModelAndView("/");
        try {
            Subject subject = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
            subject.login(token);

            UserInfo user = userService.findByName(username);
            //在session中保存当前用户的个人信息
            session.setAttribute("SYS_CURRENT_USER", user);
            //在session中保存用户的r_path
//            Set<String> rpathSet = userService.loadUserRPath(user.getId());
//            String rpath = StringUtils.join(rpathSet, "DATA_SPLIT_CHAR");
//            session.setAttribute("SYS_CURRENT_USER_RPATH", rpath);

        } catch (IllegalArgumentException e) {
            mv.setViewName("/login/index");
            mv.addObject("errMsg","参数异常");
            logger.error(e.getMessage(), e.getStackTrace());
        } catch (AuthenticationException e) {
            logger.error(e.getMessage(), e.getStackTrace());
            mv.setViewName("/login/index");
            mv.addObject("errMsg","认证失败");
//        } catch (BusinessException e) {
//            mv.setViewName("/login/index");
//            mv.addObject("errMsg","登录异常");
//
//            logger.error(e.getMessage(), e.getStackTrace());
        } catch (Exception e) {
            mv.setViewName("/login/index");
            mv.addObject("errMsg","登录异常");
            logger.error(e.getMessage(), e.getStackTrace());
        }
        return mv;
    }

}
